package com.overlord.im.connector.mq.listener;

import com.overlord.im.commons.message.OverlordImMessage;
import com.overlord.im.commons.mq.ListenerSelector;
import com.overlord.im.connector.netty.connector.ConnectorContext;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

/**
 * @ClassName RedirectMessageListener
 * @Author wuwei
 * @Description NONE
 * @Date 2020/6/2 14:40
 **/
@Service
@Slf4j
public class RedirectMessageListener {

    @RabbitListener(queues = {"#{redirectMsgQueue}"}, containerFactory = ListenerSelector.MULTI_THREAD)
    public void listener(OverlordImMessage message) {
        log.info("接收到转发消息:{}", message);
        ChannelHandlerContext ctx = null;
        switch (message.getAckType()) {
            case MSG_A:
            case ACK_N:
                ctx = ConnectorContext.channelContext(message.getFrom());
                break;
            case MSG_N:
            case ACK_A:
                ctx = ConnectorContext.channelContext(message.getTo());
                break;
        }
        if (null != ctx) {
            ctx.writeAndFlush(message);
        }
    }

}
